package step.xmpp.base;

import java.io.IOException;

import javax.net.SocketFactory;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.Builder;
import org.junit.After;
import org.junit.Test;

public class SendUtils {
	private XMPPTCPConnection connection;
    private ConnectionConfiguration config;
    /** openfire服务器address */
    private final static String server = "192.168.254.129";
    public static XMPPTCPConnection getConnection(String userName,String password){
    	XMPPTCPConnection connection;
    	Builder builder = XMPPTCPConnectionConfiguration.builder();
        builder.setHost(server);
        builder.setServiceName(server);
        builder.setPort(5222);
        builder.setUsernameAndPassword(userName, password);
        builder.setDebuggerEnabled(true);
        builder.setCompressionEnabled(true);
        builder.setSecurityMode(SecurityMode.disabled);//不支持TLS验证
        connection=new XMPPTCPConnection(builder.build());
        try {
        	if(connection.isConnected()){
        		
        	}else{
        		connection.connect();
        	}
        		
        	connection.login();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return connection;
    }
    /**
     * <b>function:</b> 初始Smack对openfire服务器链接的基本配置
     * @author hoojo
     * @createDate 2012-6-25 下午04:06:42
     */
    public void init() {
        try {
            Builder builder = XMPPTCPConnectionConfiguration.builder();
            builder.setHost(server);
            builder.setServiceName(server);
            builder.setPort(5222);
            builder.setUsernameAndPassword("opentest1", "123456");
            builder.setDebuggerEnabled(true);
            builder.setCompressionEnabled(true);
//            builder.setResource("web");
            builder.setSecurityMode(SecurityMode.disabled);//不支持TLS验证
            XMPPTCPConnectionConfiguration build = builder.build();
            connection = new XMPPTCPConnection(build);
            AbstractXMPPConnection connect = connection.connect();
            String user = connect.getUser();
            connect.login();
            System.out.println(connect.getHost());
            System.out.println(user);
            ChatManager chatmanager = ChatManager.getInstanceFor(connection);
            Chat newChat = chatmanager.createChat("opentest2@"+server,new ChatMessageListener() {
				public void processMessage(Chat chat, Message message) {
				}
			});
            try {
            	newChat.sendMessage("您好,请问你是小黑吗?");
            }
            catch (Exception e) {
            	System.out.println("Error Delivering block");
            }
			while (true) {

			}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @After
    public void destory() {
        if (connection != null) {
//            connection.disconnect();
            connection = null;
        }
    }
}
